const path = require('path')
const webpack = require('webpack')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const dllConfig = require('./dll.config.js')
const fs = require('fs-extra')
const WebpackBar = require('webpackbar')
const chalk = require('chalk')
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin')

fs.ensureDirSync(dllConfig.path)

module.exports = {
  // 入口文件
  entry: {
    ...dllConfig.library
  },
  // 输出文件
  output: {
    path: dllConfig.path,
    filename: '[name].js',
    library: '[name]_[hash]'
  },
  plugins: [
    new WebpackBar({
      name: 'build-dll',
      reporter: {
        start(context) {
          console.log(chalk.yellow('开始构建...'))
        },
        allDone(context) {
          // Called when _all_ compiles finished
          console.log(chalk.yellow('构建成功...'))
        }
      }
    }),
    // 清除之前的dll文件
    new CleanWebpackPlugin(),
    new webpack.DllPlugin({
      path: path.join(dllConfig.path, '[name]-manifest.json'),
      name: '[name]_[hash]'
    }),
    new FriendlyErrorsWebpackPlugin({
      // 成功的时候输出
      compilationSuccessInfo: {
        messages: [`dll文件存放在 ${dllConfig.path}`]
      }
    })
  ],
  performance: {
    hints: false
  }
}
